<template>
    <el-row class="warp">
        <el-col class="linkagenav publicNav" :span="3" style="height:100%;">
          <router-link class="newnava" :to="{'path':''}">联动管理</router-link>
        </el-col>
         <el-col :span="21" style="height:100%">
          <div class="main-contents-box">
            <div class="main-contents-main">
                <div class="devicewarp">
                    <template>
                      <div class="newadd">
                          <el-button icon="el-icon-circle-plus-outline" type="primary" size='mini' @click="addlinkage" >新增联动</el-button>
                      </div>
                      <div class="linkage-talbe-warp" v-loading="this.$store.state.linkLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.7)">
                              <el-table class="linkage"
                                :data="tableData1"
                                border
                                size="mini"
                                empty-text="暂无联动信息"
                                style="width: 100%;text-align: center"
                                >
                                    <el-table-column
                                      fixed
                                      prop="alarmsidname"
                                      label="告警设备"
                                      show-overflow-tooltip
                                      header-align="center"
                                      >
                                      
                                    </el-table-column>
                                    <el-table-column
                                      prop="dname"
                                      label="告警设备名称"
                                      show-overflow-tooltip
                                      header-align="center"
                                      width='120'
                                      >
                                      <div v-for="item in tableData1" style="display:none">
                                        {{item.lid}}
                                      </div>
                                    </el-table-column>
                                    
                                    <el-table-column
                                      prop="alarmOrder"
                                      label="告警指令"
                                      show-overflow-tooltip
                                      header-align="center"
                                      width='90'
                                      >
                                    </el-table-column>
                                  <el-table-column
                                    prop="ctrlsidname"
                                    label="联动设备"
                                    show-overflow-tooltip
                                    header-align="center"
                                    width='120'
                                    >
                                  </el-table-column>
                                  <el-table-column
                                      prop="ctrldname"
                                      label="联动设备名称"
                                      show-overflow-tooltip
                                      header-align="center"
                                      width='120'
                                      >
                                  </el-table-column>
                                  <el-table-column
                                    prop="ctrlOrder"
                                    label="联动指令"
                                    show-overflow-tooltip
                                    header-align="center"
                                    >
                                  </el-table-column>
                                  <el-table-column
                                    prop="freesidname"
                                    label="解除设备"
                                    show-overflow-tooltip
                                    header-align="center"
                                    >
                                  </el-table-column>
                                  <el-table-column
                                      prop="freedname"
                                      label="解除设备名称"
                                      show-overflow-tooltip
                                      header-align="center"
                                      width='120'
                                      >
                                  </el-table-column>
                                  <el-table-column
                                    prop="freeOrder"
                                    label="解除指令"
                                    show-overflow-tooltip
                                    header-align="center"
                                    >
                                  </el-table-column>
                                  <el-table-column fixed="right" width='100' label="操作" header-align="center">
                                      <template slot-scope="scope">
                                        <el-button @click="linkageDelete1(scope.$index, scope.row)"  type="text" size="small">删除</el-button>
                                      </template>
                                  </el-table-column>
                              </el-table>
                          </div>
                    </template>
                </div>
            </div>
          </div>
            <div v-if="linkShow">
              <linkagePopup @on-close="closelinkage" :is-show="linkageShow" @safe="loadings"  @save-types="saveTypes"></linkagePopup>
            </div>
         </el-col>
    </el-row>
</template>
<script>
import linkagePopup from  "./Monitornr/tem/linkagepopup.vue"
import {get_chart_sensorlist,get_attr1,get_orderName,get_alarmOption} from "../js/public.js"
export default {
    name: 'linkage',
    components:{
      linkagePopup
    },
  data () {
    return {
      msg: '我是联动系统',
      linkageShow:false,
      loading:false,
      linkTable: [],
      linkShow:false,
      tableData1:[]
    }
  },
  methods:{
    //新增点击触发
    addlinkage(){
        this.$data.linkageShow=true;
        this.$data.linkShow=true;
    },
    //关闭联动弹窗触发
    closelinkage(){
      this.$data.linkageShow=false;
      this.$data.linkShow=false;
    },
    //数据载入触发
    loadings(){
      this.$data.linkageShow=false;
    },
    //弹窗保存新增触发device设备数据更新
    saveTypes(value){
      this.$data.tableData1.push(value);
      this.$data.linkageShow=false;
      this.$data.linkShow=false;
    },
    get_pwd(){
        let  pwd; 
        $.ajax({
            url:'../cfg/password.json',
            type:'GET',
            cache:false,
            async: false,
            dataType:'text',
            success:function(result){
                let data = eval("("+result+")");
                pwd = data.password;
            },
            error:function(realdata){                           
            }
        })
        return pwd;
    },
    check_pwd(){
        let username = window.sessionStorage.getItem('username');
        let password = window.sessionStorage.getItem('password');
        let pwd = this.get_pwd();
        if(username == "admin"){
            if(password != pwd){
                this.$message({
                    message: '密码做了更改,请重新登录!',
                    type: 'info',
                    center:true,
                    showClose: true
                });
                this.loginout();
            }
        } 
    },
    loginout(){
        window.sessionStorage.removeItem("username");
        window.sessionStorage.removeItem("password");
        this.set_log("用户退出");
        this.$router.push("/login");
    },
    set_log(str){//用户操作日志
        let  msg= {"cmd":"CGI_LOG_SET_OPT","content":str};
        console.info(msg);
        $.ajax({
            url:"cgi-bin/main.cgi",
            contentType: "application/json;charset=utf-8",
            type:"post",
            data:JSON.stringify(msg),
            success:function(msg){           
            }
        })
    },
    //获取数据列表可以不需要改变
    get_state(){
      let that=this;

      let msg = {"cmd":"CGI_LINKAGE_GET_LIST"};

      if(window.sessionStorage.getItem('username')=="zyl"){
          this.$ajax.get("./static/getlinkage.json").then(function(res){
              let abc=res.data;

              $.each(abc,function(i,value){
                  let sid = value.did>>16;
                  let ctrlsid = value.ctrldid>>16;
                  let freesid  = value.freedid>>16;


                abc[i].alarmsidname=get_chart_sensorlist(sid);
                abc[i].alarmOrder=get_attr1(value.mid)+get_alarmOption(value.alarm);
                
                abc[i].ctrlsidname=get_chart_sensorlist(ctrlsid);
                abc[i].ctrlOrder=get_orderName(value.ctrlcid);
                if(value.freeenable==false){
                  abc[i].freesidname="-";
                  abc[i].freeOrder="-";
                  abc[i].freedname="-";
                }else{
                  abc[i].freesidname=get_chart_sensorlist(freesid);
                  abc[i].freeOrder=get_orderName(value.freecid);
                }
              });
              that.$data.tableData1=abc;
              /*if(that.$data.tableData1.length==0){
                $(".el-table__body-wrapper").css("display","none")
              }else{
                 $(".el-table__body-wrapper").css("display","block")
              }*/
          })

      }else{
          this.$ajax.post("./cgi-bin/main.cgi",msg).then(function(res){

           let abc=res.data;
           $.each(abc,function(i,value){
              let sid = value.did>>16;
              let ctrlsid = value.ctrldid>>16;
              let freesid  = value.freedid>>16;

              abc[i].alarmsidname=get_chart_sensorlist(sid);
              abc[i].alarmOrder=get_attr1(value.mid)+get_alarmOption(value.alarm);
              
              abc[i].ctrlsidname=get_chart_sensorlist(ctrlsid);
              abc[i].ctrlOrder=get_orderName(value.ctrlcid);
              if(value.freeenable==false){
                abc[i].freesidname="-";
                abc[i].freeOrder="-";
                abc[i].freedname="-";
              }else{
                abc[i].freesidname=get_chart_sensorlist(freesid);
                abc[i].freeOrder=get_orderName(value.freecid);
              }
           });
           that.$data.tableData1=abc;
           /*if(that.$data.tableData1.length==0){
              $(".el-table__body-wrapper").css("display","none")
           }else{
              $(".el-table__body-wrapper").css("display","block")
           }*/
      })

      }
     
    },
    //点击删除联动
    linkageDelete1(index, row) {
      
      let that=this;
      this.$confirm('此操作将删除该联动记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        let msg = {"cmd":"CGI_LINKAGE_SET_DELETE","lid":row.lid};
        console.log(row);
        console.log(row.lid);
        console.log(msg);
        if(window.sessionStorage.getItem("username") == "zyl"){

            that.$ajax.get("./static/requext.json",msg).then(function(res){
                if(res.data.response==true){
                  that.$message({
                    type: 'success',
                    message: '删除成功'
                  })
                  //删除成功后页面进行的操作
                  that.$data.tableData1.splice(index,1)
                  if(that.$data.tableData1.length==0){
                    $(".el-table__body-wrapper").css("display","none")
                  }else{
                    $(".el-table__body-wrapper").css("display","block")
                  }
                }else{
                  that.$message({
                    type: 'error',
                    message: '删除失败',
                  });
                }
            })

        }else{

            $.ajax({
                url:'cgi-bin/main.cgi',
                type:'post',
                cache:false,
                async: false,
                data:JSON.stringify(msg),
                success:function(res){
                    let data = eval("("+res+")");

                    if(data.response==true){
                        that.$message({
                            type: 'success',
                            message: '删除成功'
                        })
                        //删除成功后页面进行的操作
                        that.$data.tableData1.splice(index,1)
                        if(that.$data.tableData1.length==0){
                          $(".el-table__body-wrapper").css("display","none")
                        }else{
                          $(".el-table__body-wrapper").css("display","block")
                        }
                      }else{
                        that.$message({
                          type: 'error',
                          message: '删除失败',
                        });
                      }
                         
                  },
                  error:function(realdata){
                  }
              })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
          position:'bottom-right'
        });
      });
    },
  },
  mounted(){
     this.check_pwd(); 
  },
  //页面创建修改请求数据
  created(){
    this.get_state();
  }
}
</script>
<style>
.devicewarp{
  width:80%;
  min-width: 500px;
  max-height: 600px;
  min-height: 300px;
  margin: auto;
  height: auto;
  padding-top: 50px;
}
.newadd{
   width:100%;
   height: 50px;
   margin: auto;
   margin-bottom: 10px;
}
.newadd button{
  float: right;
}
.linkage{
  z-index: 0;
}
.linkage td{
  padding: 5px;
}
.linkage-talbe-warp{
  width: 100%;
  min-height: 500px;
}
</style>
